package com.kotlin.android.simple.simple2

import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.kotlin.android.core.BaseVMActivity
import com.kotlin.android.ktx.ext.*
import com.kotlin.android.ktx.utils.LogUtils
import com.kotlin.android.liveevent.event.LoginState
import com.kotlin.android.mtime.ktx.ext.progressdialog.dismissProgressDialog
import com.kotlin.android.mtime.ktx.ext.progressdialog.showProgressDialog
import com.kotlin.android.router.path.RouterActivityPath
import com.kotlin.android.simple.R
import kotlinx.android.synthetic.main.simple_main.*

/**
 *
 * Created on 2020/4/24.
 *
 * @author o.s
 */
@Route(path = RouterActivityPath.Simple.PAGER_SIMPLE_MVVM2)
class SimpleMVVMActivity2 : BaseVMActivity<SimpleViewModel2>(false) {


    override fun getLayoutResId() = R.layout.simple_main

    override fun initVM() = viewModels<SimpleViewModel2>().value

    override fun initView() {
        simpleRequest?.setOnClickListener {
            mViewModel?.loadData()
        }
        intent.getIntExtra(KEY_SCREEN_WIDTH, 0).v()
        intent.getStringExtra(KEY_SCREEN_HEIGHT).v()
        val loginState = intent.getSerializableExtra(KEY_EXTRA) as LoginState
        LogUtils.v("${loginState.isLogin}")
        setResult(321)
    }

    override fun initData() {
    }

    override fun startObserve() {
        mViewModel?.apply {
            uiState.observe(this@SimpleMVVMActivity2, Observer {
                it.apply {
                    if (showLoading) {
                        showProgressDialog()
                    } else {
                        dismissProgressDialog()
                    }

                    success?.apply {
                        // TODO 成功
                        simpleResponse?.text = it.toString()
                    }

                    error?.apply {
                        // TODO 错误
                        simpleResponse?.text = this
                    }

                    netError?.apply {
                        // TODO 网络错误
                        simpleResponse?.text = this
                    }

                    if (needLogin) {
                        // TODO 去登陆
                        toast("需要登录")
                    }
                    if (isEmpty) {

                    }
                }
            })
        }
    }


}